<?php
$this->breadcrumbs=array(
	'Articles'=>array('index'),
	'Create',
);

$this->menu=array(
	array('label'=>'List Article', 'url'=>array('index')),
	array('label'=>'Manage Article', 'url'=>array('admin')),
);
?>

<h1>发表日志</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
<script type="text/javascript">

var temp;
//显示/隐藏文本内容
function showNext(object){

	if(object.attr("alt") != '隐藏') {
		object.attr("alt", "隐藏");
		object.attr("title", object.text());
		object.text(object.attr("alt"));
	}else{
		object.text(object.attr("title"));	
		object.attr("title",object.attr("alt"));
		object.attr("alt", object.text());
			
	}
	
	console.log(object.text());

	object.next().slideToggle("slow");
	
	return false;
}
//改变标签内容
function changeText(object, id){
	var label = object.find("option:selected").text();
//	alert(id.html()+label);
	id.text(label);
}

function checkboxChange()
{
//
	$("input[name=checkItem][type=checkbox]").change(function(){
//		alert($(this).val());
		
//		alert($("#Article_arc_cids").val());
		var str = "";
		$("input[name=checkItem][type=checkbox]:checked").each(function(){
		
			console.log($(this).attr("checked"));
//			if($(this).attr("checked") == true){
				str += ","+$(this).val();
//			}
		});
		
		str = str.substring(1,str.length);
		

		$("#Article_arc_cids").val(str);

	});	
}

//获取当前编辑文本相关信息，并格式化
function showValues() {
    var fields = $(":input").serializeArray();
    $("#results").empty();
    $.each(fields, function(i, field){
      $("#results").append(field.name+":"+addSlash(field.value) + "|#-#-#-#|");
    });
//    $("#Article_arc_result").val($("#results").html());
    
  }

  function addSlash(val){
		val = val.replace(/:/g,"\\:");
		val = val.replace(/;/g,"\\;");

//		alert(val);
		return val;
  }

//自动保存
  function AutoSave(){

	  var data = $("#article-form").serializeArray();

	  console.log(data);
	  
		$.post("<?php echo $this->createUrl('/service/autosave'); ?>", data, function(msg){
			$("#autosave").html(msg);
		});		
  }

  window.setInterval("AutoSave()",18000);

  function getTags()
  {
		var content = UE.getEditor('editor').getContent();
	  
		$.post('<?php echo $this->createUrl('/service/gettags'); ?>',{'content':content},function(msg){
			$("#Article_arc_tags").val(msg);
		});
  }

$(function(){


	$("body").mousemove(function(){
		$("#Article_arc_content").val(UE.getEditor('editor').getContent());
	});

	
	var str = "这里填写文章标题";
	$("#Article_arc_title").val(str).focus(function(){
		if($(this).val()==str){
			$(this).val("");
		}		
	});

	//checkbox onChange事件
	checkboxChange();


	//提交
	$("#article-form").submit(function(){

		$.post('<?php echo $this->createUrl('create'); ?>', $(this).serializeArray(), function(data){
			console.log(data);

			if(data.state == 'success'){

				$(".errorSummary").hide();

				var dialog = art.dialog({
					title:'欢迎',
					lock:true,
					fixed: true,
					content:data.message,
					resize:true,
					button:[{
						name: '预览', 
						callback: function () {
							var object = this;
							art.dialog.open(data.redirect, {title: '预览', width: '90%', height: '90%',fixed: true,});					
							return false;
						}
					}, {
						name: '继续发布文章',
						callback:function(){

							location.reload();

						}
					}] ,				

			});

			}else{

				$(".errorSummary").html($(data.adds)).show();
				location.reload();
			}
			
		},'json');
				
		return false;
	});

	
	$("#add_category").click(function(){
		
		$.post("<?php echo $this->createUrl('category/create');?>",{Category:{
			'cate_pid':$("#Article_arc_cid").val(),
			'cate_name':$("#categoryName").val(),
			'cate_type':<?php echo Category::CATEGORY_NEWS;?>,
			'cate_contype':<?php echo Category::CONTENT_STORY;?>,
			'cate_recommend':<?php echo Category::CATEGORY_RECOMMEND_CANCEL;?>,
			'cate_des':'无',
			'cate_order':<?php echo rand(10, 500);?>
			
		}},function(data){
//			alert(msg);
//			$(".category").append(msg);

			console.log(data);

			if(data.state == 'success'){

				$("#categoryList").html("loading.......");
				
				$.get('<?php echo $this->createUrl('category/list');?>',{'id':<?php echo Category::CATEGORY_NEWS;?>},function(msg){
					console.log(msg);
					$("#categoryList").html($(msg).html());
				});			

				$.getJSON("<?php echo $this->createUrl('category/dropdownlist');?>",{'id':<?php echo Category::CATEGORY_NEWS;?>},function(msg){
//			 	      alert(msg);
							
				            var pid = document.getElementById("Article_arc_cid");

				            //清除所有已有选项
				            while(pid.childNodes.length>0){
				            	pid.removeChild(pid.childNodes[0]);
				           }
				           $.each(msg,function(k,v){
//								dd += (k+v);

				            					
				            	var option = new Option(v,k);
				            	pid.options.add(option);
				            });
//				            alert(dd);

					       	//checkbox onChange事件
					       	checkboxChange();
				    	});		
	

			}else{


				
			}			
			

		},'json');

	});
});
</script>